package com.cloud.phone.task;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.GestureDescription;
import android.graphics.Rect;
import android.view.accessibility.AccessibilityNodeInfo;

import com.cloud.phone.service.AccessibilityAutoMainService;
import com.cloud.phone.tools.AccessibilityTool;
import com.cloud.phone.tools.LogUtils;

import java.util.List;

public abstract class BaseDealTools {
    public AccessibilityAutoMainService service;
    public AccessibilityTool accessibilityTool;
    public BaseTask baseTask;
    public BaseDealTools(BaseTask task) {
        this.accessibilityTool = AccessibilityTool.getInstance();
        this.baseTask=task;
    }

    public boolean clickXy(AccessibilityNodeInfo accessibilityNodeInfo){
        if(baseTask!=null&&baseTask.taskStatus==BaseTask.STOP){
            return false;
        }
        if(accessibilityNodeInfo==null){
            return false;
        }
        Rect rect = new Rect();
        accessibilityNodeInfo.getBoundsInScreen(rect);
        float left = rect.left;
        float right = rect.right;
        float top = rect.top;
        float bottom = rect.bottom;
        int x = (int) ((left + right) / 2);
        int y = (int) ((top + bottom) / 2);
        if(y<0){
            y= (int) (Math.abs(top-bottom)*2);
        }

        LogUtils.i("点击坐标："+x+","+y+"|---"+left+","+top+","+right+","+bottom+"|className="+accessibilityNodeInfo.getClassName());
        boolean isClickSuccess=accessibilityTool.clickScreen(service,x,y);
        if(!isClickSuccess){
            LogUtils.w("点击坐标失败了,尝试点击控件");
            return click(accessibilityNodeInfo);
        }else {
            LogUtils.i("点击坐标成功了");
            return true;
        }
    }


    public boolean longClickXy(AccessibilityNodeInfo accessibilityNodeInfo){
        if(baseTask!=null&&baseTask.taskStatus==BaseTask.STOP){
            return false;
        }
        if(accessibilityNodeInfo==null){
            return false;
        }
        Rect rect = new Rect();
        accessibilityNodeInfo.getBoundsInScreen(rect);
        float left = rect.left;
        float right = rect.right;
        float top = rect.top;
        float bottom = rect.bottom;
        int x = (int) ((left + right) / 2);
        int y = (int) ((top + bottom) / 2);
        if(y<0){
            y= (int) (Math.abs(top-bottom)*2);
        }

        LogUtils.i("点击坐标："+x+","+y+"|---"+left+","+top+","+right+","+bottom+"|className="+accessibilityNodeInfo.getClassName());
        boolean isClickSuccess=accessibilityTool.longClickScreen(service,x,y);
        if(!isClickSuccess){
            LogUtils.w("点击坐标失败了,尝试点击控件");
            return longClick(accessibilityNodeInfo);
        }else {
            LogUtils.i("点击坐标成功了");
            return true;
        }
    }


    public boolean longClick(AccessibilityNodeInfo accessibilityNodeInfo){
        if(baseTask!=null&&baseTask.taskStatus==BaseTask.STOP){
            return false;
        }
        boolean isClicked = accessibilityTool.longClick(accessibilityNodeInfo);
        if(!isClicked){
            LogUtils.w("点击控件失败了,尝试点击坐标");
            return clickXy(accessibilityNodeInfo);
        }else {
            LogUtils.i("点击控件成功了");
            return true;
        }
    }


    public boolean click(AccessibilityNodeInfo accessibilityNodeInfo){
        if(baseTask!=null&&baseTask.taskStatus==BaseTask.STOP){
            return false;
        }
        boolean isClicked = accessibilityTool.click(accessibilityNodeInfo);
        if(!isClicked){
            LogUtils.w("点击控件失败了,尝试点击坐标");
            return clickXy(accessibilityNodeInfo);
        }else {
            LogUtils.i("点击控件成功了");
            return true;
        }
    }

    public AccessibilityNodeInfo getRootNodeInfo() {
        return accessibilityTool.getRootNodeInfo(service);
    }

    public AccessibilityNodeInfo findNodeFirstById(String id){
        if(baseTask!=null&&baseTask.taskStatus==BaseTask.STOP){
            return null;
        }
        return accessibilityTool.findNodeFirstById(service,id);
    }

    public AccessibilityNodeInfo findNodeFirstPlusById(String id){
        if(baseTask!=null&&baseTask.taskStatus==BaseTask.STOP){
            return null;
        }
        List<AccessibilityNodeInfo> nodes = accessibilityTool.findNodesByIdPlus(null, null, service, id);
        if(!isNodeExist(nodes)){
            return null;
        }
        return nodes.get(0);
    }

    public AccessibilityNodeInfo findNodeIndexById(String id,int index){
        if(baseTask!=null&&baseTask.taskStatus==BaseTask.STOP){
            return null;
        }
        return accessibilityTool.findNodeIndexById(service,index,id);
    }

    public AccessibilityNodeInfo findNodeIndexPlusById(String id,int index){
        if(baseTask!=null&&baseTask.taskStatus==BaseTask.STOP){
            return null;
        }
        List<AccessibilityNodeInfo> nodes = accessibilityTool.findNodesByIdPlus(null, null, service, id);
        if(!isNodeExist(nodes)){
            return null;
        }
        return nodes.get(index);
    }


    public List<AccessibilityNodeInfo> findNodesPlusById(String id){
        if(baseTask!=null&&baseTask.taskStatus==BaseTask.STOP){
            return null;
        }
        List<AccessibilityNodeInfo> nodes = accessibilityTool.findNodesByIdPlus(null, null, service, id);
        return nodes;
    }

    public AccessibilityNodeInfo findNodeFirstById(AccessibilityNodeInfo nodeInfo,String id){
        if(baseTask!=null&&baseTask.taskStatus==BaseTask.STOP){
            return null;
        }
        return accessibilityTool.findNodeFirstById(nodeInfo,id);
    }

    public List<AccessibilityNodeInfo> findNodesById(String id){
        if(baseTask!=null&&baseTask.taskStatus==BaseTask.STOP){
            return null;
        }
        return accessibilityTool.findNodesById(service,id);
    }

    public List<AccessibilityNodeInfo> findNodeInfoByClassName(AccessibilityNodeInfo accessibilityNodeInfo,String className){
        if(baseTask!=null&&baseTask.taskStatus==BaseTask.STOP){
            return null;
        }
        return accessibilityTool.findNodeInfoByClassName(null,accessibilityNodeInfo,service,className);
    }

    public String getCurrentPagePackageName(){
        return accessibilityTool.getCurrentPagePackageName(service);
    }


    public void setNodeText(AccessibilityNodeInfo node, String text) {
        accessibilityTool.setNodeText(node,text);
    }

    public void backOnce(){
        if(baseTask!=null&&baseTask.taskStatus==BaseTask.STOP){
            return;
        }
        accessibilityTool.backOnce(service);
    }

    public List<AccessibilityNodeInfo> findNodesByText(String keywords){
        if(baseTask!=null&&baseTask.taskStatus==BaseTask.STOP){
            return null;
        }
        return accessibilityTool.findNodesByText(null,null,service,keywords);
    }

    public boolean isNodeExist(List<AccessibilityNodeInfo> nodeInfos){
        return accessibilityTool.isNodeExist(nodeInfos);
    }

    public void swipe(int x, int y, int lastX, int lastY, long duration) {
        accessibilityTool.swipe(service, x, y, lastX, lastY, duration, new AccessibilityService.GestureResultCallback() {
            @Override
            public void onCompleted(GestureDescription gestureDescription) {
                super.onCompleted(gestureDescription);
            }
        });
    }

    public void swipeNodeInfo(int count,AccessibilityNodeInfo accessibilityNodeInfo) {
        Rect rect = new Rect();
        accessibilityNodeInfo.getBoundsInScreen(rect);
        float left = rect.left;
        float right = rect.right;
        float top = rect.top;
        float bottom = rect.bottom;
        int x = (int) ((left + right) / 2);
        int y = (int) ((top + bottom) / 2);
        if(y<0){
            y= (int) (Math.abs(top-bottom)*2);
        }
        int startX = (int) ((left+x)/ 2);
        int endX = (int) ((right+x)/2);
        int startY = (int) ((bottom+y)/2);
        int  endY= (int) ((top+y)/2);
        LogUtils.v("startY=" + startY + ",endY=" + endY + ",startX=" + startX + ",endX=" + endX);
        swipe(startX, startY, endX, endY, 700);
        try {
            Thread.sleep(1000);
            count--;
            if(count>0){
                swipeNodeInfo(count,accessibilityNodeInfo);
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

}
